/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gateway.server.disruptor;

import com.gateway.disruptor.DisruptorProviderManager;
import com.gateway.disruptor.provider.DisruptorProvider;
import com.gateway.register.client.server.api.GatewayClientServerRegisterPublisher;
import com.gateway.register.common.type.DataTypeParent;
import com.gateway.server.disruptor.executor.RegisterServerConsumerExecutor;
import com.gateway.server.disruptor.subscriber.MetadataExecutorSubscriber;
import com.gateway.server.disruptor.subscriber.RouteRegisterExecutorSubscriber;
import com.gateway.server.service.GatewayClientRegisterService;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * The type Disruptor publisher.
 */
public class RegisterClientServerDisruptorPublisher implements GatewayClientServerRegisterPublisher {

    private static final RegisterClientServerDisruptorPublisher INSTANCE = new RegisterClientServerDisruptorPublisher();

    private DisruptorProviderManager<Collection<DataTypeParent>> providerManager;

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static RegisterClientServerDisruptorPublisher getInstance() {
        return INSTANCE;
    }

    /**
     * start.
     *
     * @param gatewayClientRegisterServiceMap the gateway client register service
     */
    public void start(final Map<String, GatewayClientRegisterService> gatewayClientRegisterServiceMap) {
        RegisterServerConsumerExecutor.RegisterServerExecutorFactory factory = new RegisterServerConsumerExecutor.RegisterServerExecutorFactory();
        factory.addSubscribers(new MetadataExecutorSubscriber(gatewayClientRegisterServiceMap));
        factory.addSubscribers(new RouteRegisterExecutorSubscriber(gatewayClientRegisterServiceMap));
        providerManager = new DisruptorProviderManager<>(factory);
        providerManager.startup();
    }

    @Override
    public void publish(final DataTypeParent data) {
        DisruptorProvider<Collection<DataTypeParent>> provider = providerManager.getProvider();
        provider.onData(Collections.singleton(data));
    }

    @Override
    public void publish(final Collection<? extends DataTypeParent> dataList) {
        DisruptorProvider<Collection<DataTypeParent>> provider = providerManager.getProvider();
        provider.onData(dataList.stream().map(DataTypeParent.class::cast).collect(Collectors.toList()));

    }

    @Override
    public void close() {
        providerManager.getProvider().shutdown();
    }
}
